package com.itic.system.self.selfLifting.service;

import com.itic.appbase.framework.Base.BaseCode;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.type.SelfliftingOrderType;
import com.itic.system.self.selfLifting.persistence.dao.SelfLiftingDao;
import com.itic.system.self.selfLifting.persistence.model.SelfLifting;
import com.itic.system.self.selfLifting.persistence.model.SelfLiftingExample;
import com.itic.system.self.selfLifting.persistence.model.SelfLiftingExample.Criteria;
import com.itic.system.vo.SelfliftDelCon;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


@Service
@Transactional
public class SelfLiftingService
     extends CrudService<SelfLiftingDao,SelfLifting,SelfLiftingExample> {
    @Autowired
    private SelfLiftingDao selfLiftingDao;
    
    /**
     * 根据查询条件查询
     * @param userId
     * @param name
     * @param page
     * @param type
     * @return
     */
    public List<SelfLifting> quryBycondition(String userId,String name,String type){
    	List<SelfLifting> result = null;
    	SelfLiftingExample example = new SelfLiftingExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andUserIdEqualTo(userId);
    	if(StringUtils.isNotBlank(name)){
    		criteria.andNameLike("%"+name+"%");
    	}
    	if(StringUtils.isNotBlank(type)){
    		if(type.equals(SelfliftingOrderType.group.getKey())){
    			example.setOrderByClause("BIG_GROUP asc");
    		}
    		if(type.equals(SelfliftingOrderType.order.getKey())){
    			example.setOrderByClause("ORDER_COUNT asc");
    		}
    		if(type.equals(SelfliftingOrderType.sale.getKey())){
    			example.setOrderByClause("TOTAL_SALE asc");
    		}
    	}
    	result = this.list(example); 
    	return result;
    }
    public void delByuserIdandId(SelfliftDelCon con){
    	this.selfLiftingDao.delbyuserIdandid(con);
    }
    /**
     * 校验是否为本人数据
     * @param userId
     * @param id
     * @return
     */
    public Boolean isOwn(String userId,String id){
    	Boolean isOwn = false;
    	SelfLiftingExample example = new SelfLiftingExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andUserIdEqualTo(userId);
    	criteria.andIdEqualTo(id);
    	List<SelfLifting> list = this.list(example);
    	if(list.size()>0){
    		isOwn = true;
    	}
    	return isOwn;
    }
    
    /**
     * 根据用户Id获取列表
     * @param userId
     * @return
     */
    public List<SelfLifting> getListByUserId(SelfLiftingExample example,String userId){
    	List<SelfLifting> list = null;
    	if(StringUtils.isNotBlank(userId)){
    		Criteria criteria = example.createCriteria();
    		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    		criteria.andUserIdEqualTo(userId);
    		list = this.selfLiftingDao.selectByExample(example);
    	}
    	return list;
    }
    /**
     * 添加自提点
     * @param selfName
     * @param provinceId
     * @param proviceName
     * @param cityId
     * @param cityName
     * @param regionId
     * @param regionName
     * @param address
     * @param phone
     * @param seftCode
     * @param userId
     * @return
     */
    public String addSelfLifting(String selfName,String provinceId,String proviceName,String cityId,String cityName,String regionId,String regionName,String address ,String phone ,String seftCode,String userId){
        SelfLifting selfLifting = new SelfLifting();
        selfLifting.setName(selfName);
        selfLifting.setProvinceId(provinceId);
        selfLifting.setProvinceName(proviceName);
        selfLifting.setCityId(cityId);
        selfLifting.setCityName(cityName);
        selfLifting.setRegionId(regionId);
        selfLifting.setRegionName(regionName);
        selfLifting.setAddress(address);
        selfLifting.setUserId(userId);
        selfLifting.setPhone(phone);
        selfLifting.setSelfCode(seftCode);
        String result = this.add(selfLifting);
        return result;
    }


    /**
     * 修改用户自提点业务逻辑
     * @param selfId
     * @param selfName
     * @param provinceId
     * @param proviceName
     * @param cityId
     * @param cityName
     * @param regionId
     * @param regionName
     * @param address
     * @param phone
     * @param seftCode
     * @param userId
     * @return
     */
    public Map<String,Object> editSelf(String selfId,String selfName,String provinceId,String proviceName,String cityId,String cityName,String regionId,String regionName,String address ,String phone ,String seftCode,String userId){
        Map<String,Object> resultMap = new HashMap<>();
        SelfLifting selfLifting =this.get(selfId);
        //判断自提点是否是属于当前用户
        if(selfLifting !=null && userId.equals(selfLifting.getUserId())){
            String result = this.edit(selfLifting,selfName,provinceId,proviceName,cityId,cityName,regionId,regionName,address,phone,seftCode);
            resultMap.put(BaseCode.CODE,BaseCode.SUCCESS_ERROR_CODE);
            resultMap.put(BaseCode.MSG,BaseCode.SUCCESS_ERROR_MSG);
        }else{
            resultMap.put(BaseCode.CODE,BaseCode.HPP_SELF_USER_NOT_ACCORDANCE_CODE);
            resultMap.put(BaseCode.MSG,BaseCode.HPP_SELF_USER_NOT_ACCORDANCE_MSG);
        }
        return resultMap;
    }

    /**
     * 编辑用户自提点
     * @param selfLifting
     * @param selfName
     * @param provinceId
     * @param proviceName
     * @param cityId
     * @param cityName
     * @param regionId
     * @param regionName
     * @param address
     * @param phone
     * @param seftCode
     * @return
     */
    public String  edit(SelfLifting selfLifting,String selfName,String provinceId,String proviceName,String cityId,String cityName,String regionId,String regionName,String address ,String phone ,String seftCode){
        selfLifting.setName(selfName);
        selfLifting.setProvinceId(provinceId);
        selfLifting.setProvinceName(proviceName);
        selfLifting.setCityId(cityId);
        selfLifting.setCityName(cityName);
        selfLifting.setRegionId(regionId);
        selfLifting.setRegionName(regionName);
        selfLifting.setAddress(address);
        selfLifting.setPhone(phone);
        selfLifting.setSelfCode(seftCode);
        String ressult  = this.edit(selfLifting);
        return ressult;
    }


}